<?php
require_once 'data_form'.EXT;

class investment extends data_form{

	function __construct(){
		parent::__construct();
	}
	
	function index()
	{
		$row	=	null;
		$this->load->model('investment_model');
		$row_left	=	$this->investment_model->get_by(array('customer_id' => $_SESSION['customer']['id'], 'type' => 1));
		$row_right	=	$this->investment_model->get_by(array('customer_id' => $_SESSION['customer']['id'], 'type' => 2));
		
		if($_SERVER["REQUEST_METHOD"] == "GET" && $row_left)
		{
			$left	=	array(
				'customer_id'						=>	$row_left->customer_id,
				'investment_type'			=>	$row_left->investment_type,
				'amount'					=>	$row_left->amount,
				'duration'				=>	$row_left->	duration,
				'type'	=>	$row_left->type,
				'percent_return'		=>	$row_left->percent_return,
				'explanation'						=>	$row_left->explanation,
				'id'					=>	$row_left->id
			);
			$_SESSION['data']['left']	=	$left;
		}
		
		if($_SERVER["REQUEST_METHOD"] == "GET" && $row_right)
		{
			$right	=	array(
				'customer_id'						=>	$row_right->customer_id,
				'investment_type'			=>	$row_right->investment_type,
				'amount'					=>	$row_right->amount,
				'duration'				=>	$row_right->	duration,
				'type'	=>	$row_right->type,
				'percent_return'		=>	$row_right->percent_return,
				'explanation'						=>	$row_right->explanation,
				'id'	=>	$row_right->id
			);
			$_SESSION['data']['right']	=	$right;
		}
		
		if($_SERVER["REQUEST_METHOD"]  == "POST")
		{
			$submit	=	$this->input->post('submit');
			$type	=	$this->input->post('type');
			if(!$submit)
			{
				if($type == 1)
				{
					$left	=	array(
						'customer_id'	=>	$_SESSION['customer']['id'],
						'amount'		=>	$_POST['amount1'],
						'duration'		=>	$_POST['duration1'],
						'percent_return'	=>	$_POST['percent_return1'],
						'investment_type'	=>	$_POST['investment_type1'],
						'type'	=>	$type
					);
					$_SESSION['data']['left']	=	$left;
				}//case investment on left
				else
				{
					$right	=	array(
						'customer_id'	=>	$_SESSION['customer']['id'],
						'amount'		=>	$_POST['amount2'],
						'duration'		=>	$_POST['duration2'],
						'percent_return'	=>	$_POST['percent_return2'],
						'investment_type'	=>	$_POST['investment_type2'],
						'type'	=>	$type
					);
					$_SESSION['data']['right']	=	$right;
				}//case investment on right
				
			}
			else//case calculate
			{
				$data	=	$_SESSION['data'];
				
				if(isset($data['left'])){
					if($row_left){
						
						$this->investment_model->update($row_left->id,$data['left']);
					}
					else 
					{
						$this->investment_model->insert($data['left']);
					}
				}//case investment lesf
				
				if(isset($data['right'])){
					if($row_right){
						
						$this->investment_model->update($row_right->id,$data['right']);
					}
					else 
					{
						$this->investment_model->insert($data['right']);
					}
				}
				
				
				echo "<script>window.close();</script>";
			}//case submit
			
			
		}
		$this->data['row']	=	(isset($_SESSION['data']) ? $_SESSION['data'] : null);
		$this->load->view("investment/index", $this->data);
	}

}